<?php
/**
 * 分享审核
 *
 * @copyright dongping
 * @since 2012-03-24
 */


// $_GET["e_debug"] = 1;


include_once($_SERVER["DOCUMENT_ROOT"] . "/core/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/core/admin.inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/include/foodv2/inc.php");

$g_cgival = array();
$g_pro = array();
$g_show = array();


function check_cgi_pro()
{
	global $g_cgival, $g_pro, $g_show;

	cgi::both($g_cgival["admin_check_status"], "admin_check_status", 0);
	if ($g_cgival["admin_check_status"] != 0 and $g_cgival["admin_check_status"] != 1)
	{
		$g_cgival["admin_check_status"] = 0;
	}
	cgi::both($g_cgival["status"], "status", 1);
	if ($g_cgival["status"] != 0 and $g_cgival["status"] != 1)
	{
		$g_cgival["status"] = 1;
	}
	
	
	
	
	// 搜索条件
	cgi::both($g_cgival["status_id"], "status_id", 0);
	$g_cgival["status_id"] = intval($g_cgival["status_id"]);
	
	
	cgi::both($g_cgival["search"], "search", "");
	$g_cgival["search"] = string::un_script_code($g_cgival["search"]);
	$g_cgival["search"] = string::un_html($g_cgival["search"]);
	
	cgi::both($g_cgival["uid"], "uid", "");
	if (!cgi_chk::uid($g_cgival["uid"]))
	{
		$g_cgival["uid"] = "";
	}
	
	cgi::both($g_cgival["mid"], "mid", "");
	$g_cgival["mid"] = string::un_script_code($g_cgival["mid"]);
	$g_cgival["mid"] = string::un_html($g_cgival["mid"]);
	
	cgi::both($g_cgival["start_date"], "start_time", "");
	$g_cgival["start_date"] = string::un_script_code($g_cgival["start_date"]);
	$g_cgival["start_date"] = string::un_html($g_cgival["start_date"]);

	cgi::both($g_cgival["end_date"], "end_time", "");
	$g_cgival["end_date"] = string::un_script_code($g_cgival["end_date"]);
	$g_cgival["end_date"] = string::un_html($g_cgival["end_date"]);

	
	// 分页
	cgi::both($g_cgival["p"], "p", 1);
	cgi::both($g_cgival["pcount"], "pcount", 40);
	$g_cgival["p"] = intval($g_cgival["p"]);
	$g_cgival["pcount"] = intval($g_cgival["pcount"]);	
}

function get_data()
{
	global $g_cgival, $g_pro, $g_show;
	
	$g_show = $g_cgival;
	
	$admin_check_status = $g_cgival["admin_check_status"];
	$status = $g_cgival["status"];
	$status_id = $g_cgival["status_id"];
	$search = $g_cgival["search"];
	$uid = $g_cgival["uid"];
	$mid = $g_cgival["mid"];
	$start_date = $g_cgival["start_date"];
	$end_date = $g_cgival["end_date"];

	
	// 拿到当前站点的site_id
	$site_id = admin::admin_get_user_site_id();	
	
	
	
	$db = new db_food_status();
	$db->debug = true;
	
	if ($status_id > 0)
	{
		$data = $db->admin_get_status_search_by_status_id($status_id);
		$count = 1;
		
		
		// 重置当前状态条件
		if (is_array($data) and count($data) > 0)
		{
			$g_show["admin_check_status"] = $data[0]["admin_check_status"];
			$g_show["status"] = $data[0]["status"];
		}
	}
	else
	{
		$data = $db->admin_status_search($site_id, $status, $admin_check_status, $uid, $mid, $start_date, $end_date, $search, $g_cgival["p"], $g_cgival["pcount"]);
		$count = $db->admin_status_search_count($site_id, $status, $admin_check_status, $uid, $mid, $start_date, $end_date, $search);
	}
	
	
	if (is_array($data) && strlen($search) > 0)
	{
		foreach($data as $k =>$v)
		{

			//echo "==={$search}======={$v["create_nickname"]}================\n";

			$v["create_nickname"] = str_replace($search, "<span style='color:red'>" . $search . "</span>", $v["create_nickname"]);
			$v["content"] = str_replace($search, "<span style='color:red'>" . $search . "</span>", $v["content"]);
			$data[$k] = $v;
		}
	}
	
	if (is_array($data))
	{
		$user_global_config = user_global_config::get_instance();
		
		foreach($data as $k =>$v)
		{
			
			$domain = $user_global_config->get_domain_by_site_id($v["site_id"]);
				
			if ($v["is_rstrt"] == 1)
			{
				$v["share_url"] = "http://{$domain}/explore/detail-{$v["status_id"]}.html";
			}
			else
			{
				$v["share_url"] = "http://{$domain}/show/detail-{$v["status_id"]}.html";
			}
			
			
			$v["pic_thumb"] = mod_food_status::get_current_size_pic(120, $v["pic"], $v["status_id"]);
				
			$data[$k] = $v;
		}
	}
	if (count($data) > 0)
	{
		$g_show["data"] = $data;
		$g_show["count"] = $count;
	}
	else
	{	
		$g_show["data_none"] = "1";
		$g_show["count"] = 0;
		
	}
	// Pager
	$max_page = page_bar::get_page_max($g_show["count"], $g_cgival['pcount'], $g_cgival["p"]);
	
	$page_bar_style = array(
			"curt"		=> $g_cgival["p"],
			"max"		=> $max_page,
			"key"		=> "p",
	);
	
	$g_show["page_bar"] = "<b>{$g_show["count"]}</b> 条记录&nbsp;&nbsp;第{$g_cgival["p"]}页&nbsp;共{$max_page}页&nbsp;" . page_bar::bar2($page_bar_style);
	
}

try
{
	admin_check_login();
	// 	admin_check_user_priv("foodv2/main_nav.php");

	check_cgi_pro();
	get_data();
}
catch(Exception $e)
{
	$g_show["error"] = 1;
	$g_show["errmsg"] = $e->getMessage();
}

show_pro();
exit;


function show_pro()
{
	global $g_cgival, $g_pro, $g_show;

	if (isset($_GET["debug"]))
	{
		echo "<pre>";
		print_r($g_show);
	}

	global $g_show;
	$smarty = p_get_smarty ();
	$smarty->assign ( 'g_show', $g_show );
	$smarty->display ( "foodv2/share/share_manager.html" );
}

?>